package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sumpu.app.guoguojie.dao.MailDao;
import cn.sumpu.app.guoguojie.domain.Mail;
import cn.sumpu.app.guoguojie.domain.MailLog;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class MailDaoImpl extends SqlSessionDaoSupport implements MailDao {
    private final static Logger logger = LoggerFactory.getLogger(MailDaoImpl.class);

    @Override
    public void insertMailTask(Mail mail) throws AppBusinessException {
        try {
            getSqlSession().insert("cn.sumpu.app.guoguojie.persistence.MailMapper.insertMailTask", mail);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("insertMailTask  failure.", e);
            }
            throw new AppBusinessException("insertMailTask  failure.", e);
        }

    }

    @Override
    public void updateMailTask(Mail mail) throws AppBusinessException {
        try {
            getSqlSession().update("cn.sumpu.app.guoguojie.persistence.MailMapper.updateMailTask", mail);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("updateMailTask database failure.", e);
            }
            throw new AppBusinessException("updateMailTask database failure.", e);
        }

    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Mail> getMailTask(Map<String, Object> map) throws AppBusinessException {
        try {
            return (List<Mail>) getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.MailMapper.getMailTask", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getMailTask resource failure.", e);
            }
            throw new AppBusinessException("getMailTask resource failure.", e);
        }
    }

    @Override
    public int getMailTaskCount(Map<String, Object> map) throws AppBusinessException {
        try {
            return (Integer) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.MailMapper.getMailTaskCount", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getMailTaskCount resource failure.", e);
            }
            throw new AppBusinessException("getMailTaskCount resource failure.", e);
        }
    }

    @Override
    public void insertMailTaskLog(MailLog mailLog) throws AppBusinessException {
        try {
            getSqlSession().insert("cn.sumpu.app.guoguojie.persistence.MailMapper.insertMailTaskLog", mailLog);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("insertMailTaskLog  failure.", e);
            }
            throw new AppBusinessException("insertMailTaskLog  failure.", e);
        }

    }

    @SuppressWarnings("unchecked")
    @Override
    public List<MailLog> getMailTaskLog(Map<String, Object> map) throws AppBusinessException {
        try {
            return (List<MailLog>) getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.MailMapper.getMailTaskLog", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getMailTaskLog resource failure.", e);
            }
            throw new AppBusinessException("getMailTaskLog resource failure.", e);
        }
    }

    @Override
    public int getMailTaskLogCount(Map<String, Object> map) throws AppBusinessException {
        try {
            return (Integer) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.MailMapper.getMailTaskLogCount", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getMailTaskLogCount resource failure.", e);
            }
            throw new AppBusinessException("getMailTaskLogCount resource failure.", e);
        }
    }

    @Override
    public void updateMailTaskLog(MailLog mailLog) throws AppBusinessException {
        try {
            getSqlSession().update("cn.sumpu.app.guoguojie.persistence.MailMapper.updateMailTaskLog", mailLog);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("updateMailTaskLog database failure.", e);
            }
            throw new AppBusinessException("updateMailTaskLog database failure.", e);
        }
    }

    @Override
    public void deleteMailTaskLog(Map<String, Object> map) throws AppBusinessException {
        try {
            getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.MailMapper.deleteMailTaskLog", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("deleteMailTaskLog database failure.", e);
            }
            throw new AppBusinessException("deleteMailTaskLog database failure.", e);
        }
    }

    @Override
    public void deleteMailTask(Map<String, Object> map) throws AppBusinessException {
        try {
            getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.MailMapper.deleteMailTask", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("deleteMailTask database failure.", e);
            }
            throw new AppBusinessException("deleteMailTask database failure.", e);
        }
    }
}
